package com.cybertron.ironhide.manage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cybertron.ironhide.manage.common.base.Pager;
import com.cybertron.ironhide.manage.common.jms.sender.SeedUrlMessageSender;
import com.cybertron.ironhide.manage.domain.Category;
import com.cybertron.ironhide.manage.domain.SeedUrl;
import com.cybertron.ironhide.manage.persistence.CategoryMapper;
import com.cybertron.ironhide.manage.persistence.SeedUrlMapper;
import com.cybertron.ironhide.manage.service.CategoryService;
import com.cybertron.ironhide.manage.util.CommonUtil;

@Service
public class CategoryServiceImpl implements CategoryService {

	@Resource
	private CategoryMapper categoryMapper;

	@Resource
	private SeedUrlMapper seedUrlMapper;

	@Resource
	private SeedUrlMessageSender messageSender;

	@Override
	public List<Category> selectRootCategories() {
		return categoryMapper.selectRootCategories();
	}

	@Override
	public List<Category> selectChildsByParentId(String id, Pager pager) {
		return categoryMapper.selectChildsByParentId(id, pager);
	}

	@Override
	public void insertSelective(Category record) {
		record.setId(CommonUtil.getUUID());
		record.setCreateDate(new Date());
		record.setModifyDate(new Date());
		record.setIsFinal(true);
		Category parentCate = categoryMapper.selectByPrimaryKey(record
				.getParentId());
		if (parentCate != null) {
			parentCate.setIsFinal(false);
			categoryMapper.updateByPrimaryKeySelective(parentCate);
		}
		if ("root".equals(record.getParentId())) {
			record.setTreePath(record.getId());
			record.setLevel(0);
		} else {
			record.setTreePath(parentCate.getTreePath() + "," + record.getId());
			record.setLevel(parentCate.getLevel() + 1);
		}

		record.setOrders(1);
		if (!"root".equals(record.getParentId())) {
			seedUrlMapper.updateCateIdByParentCateId(record.getId(),
					parentCate.getId());
		}
		categoryMapper.insertSelective(record);
	}

	@Override
	public List<Category> selectByParentId(String id, Pager pager) {
		return categoryMapper.selectByParentId(id, pager);
	}

	@Override
	public Category selectByPrimaryKey(String id) {
		return categoryMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateByPrimaryKeySelective(Category record) {
		record.setModifyDate(new Date());
		Category category = categoryMapper.selectByPrimaryKey(record.getId());
		if (!"root".equals(category.getParentId())) {
			Category pateCate = categoryMapper.selectByPrimaryKey(category
					.getParentId());
			record.setLevel(pateCate.getLevel() + 1);
		}
		categoryMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public void deleteByPrimaryKey(String id) {
		Category category = categoryMapper.selectByPrimaryKey(id);
		if (category == null) {
			return;
		}
		categoryMapper.deleteByPrimaryKey(id);
		seedUrlMapper.deleteByCategoryId(id);
		categoryMapper.deleteByParentId(id);
		List<Category> siblingCategories = categoryMapper
				.selectByParentId(category.getParentId());
		if (siblingCategories == null || siblingCategories.size() == 0) {
			Category parentCategory = categoryMapper
					.selectByPrimaryKey(category.getParentId());
			parentCategory.setIsFinal(true);
			categoryMapper.updateByPrimaryKey(parentCategory);
		}
	}

	@Override
	public void insertTaskSelective(String id) {
		List<Category> childs = selectTaskChilds(new ArrayList<Category>(), id);
		if (childs == null || childs.size() == 0) {
			childs = new ArrayList<Category>();
		}
		Category seedCate = categoryMapper.selectSeedsById(id);
		if (seedCate != null) {
			childs.add(seedCate);
		}
		List<SeedUrl> seedUrls = new ArrayList<SeedUrl>();
		for (Category category : childs) {
			List<SeedUrl> cateSeedUrls = category.getSeedUrls();
			if (cateSeedUrls == null || cateSeedUrls.size() == 0) {
				continue;
			}
			for (SeedUrl seedUrl : cateSeedUrls) {
				seedUrls.add(seedUrl);
			}
		}
		for (SeedUrl seedUrl : seedUrls) {
			messageSender.sendMsg(seedUrl);
		}
	}

	private List<Category> selectTaskChilds(List<Category> categories,
			String parentId) {
		List<Category> childs = categoryMapper.selectSeeds(parentId);
		for (Category cate : childs) {
			if (cate.getIsFinal()) {
				categories.add(cate);
			} else {
				selectTaskChilds(categories, cate.getId());
			}
		}
		return categories;
	}

	@Override
	public List<Category> selectByLevel(Integer level) {
		return categoryMapper.selectByLevel(level);
	}

	@Override
	public List<Category> selectByIndex() {
		return categoryMapper.selectByIndex();
	}

	@Override
	public List<Map<String, String>> selectSuggest(String title, Integer limit) {
		return categoryMapper.selectSuggest(title, limit);
	}

}
